import java.util.Vector;

import javax.microedition.lcdui.Graphics;


public class Game_src  extends Mscreen{

	
	public Cuop cuop;
	public Evils ninjaEvil, angel, flydragon, fish, boss;
	//public  Coins coin;
	public TileMap mapTile;

	//public static Vector vector = new Vector();
	
	public Game_src()
	{
	
		cuop = new Cuop(0, 0, "/cuop.png");
		mapTile = new TileMap();			
		initEvils();
		BackG.load();	
		ExGameCanvas.clear_key();
			
	}
	
	public void initEvils()
	{
		ninjaEvil = new NinjaEvil(0);
		angel = new Angel(1);
		boss = new EvilBoss(4);
		Evils.evilVec.addElement(ninjaEvil);
		Evils.evilVec.addElement(angel);
		Evils.evilVec.addElement(boss);

	}
	

	public void update()
	{

		cuop.audrop();
		cuop.update();													

		updateVectors();
		
		checkCollisonEvil();
		//checkCollisonActor();
		checkCollisonCoins();
		checkCollisonDiamond();
		
		if(Level.isNextLevel)
		{						
			startGame();
			RMS.savegame(Global.score);
		}
			
		if(Level.isResetLevel)
		{
			resetGame();
			RMS.savegame(Global.score);
		}
		
		if(Level.isOver)
		{
			reStartGame();
		}

	}
	
	
	public static void removeEvils()
	{
		Evils.evilVec.removeAllElements();
		TileMap.vector.removeAllElements();
	}
	
	public void renewEvils(int level)
	{
		switch (level) {
		case 1:
			initEvils();
			break;
		case 2:
			ninjaEvil = new NinjaEvil(0);
			angel = new Angel(1);
			boss = new EvilBoss(4);
			Evils.evilVec.addElement(boss);
			Evils.evilVec.addElement(ninjaEvil);
			Evils.evilVec.addElement(angel);
			break;
		case 3:
			ninjaEvil = new NinjaEvil(0);
			angel = new Angel(1);
			flydragon = new Flydragon(2);
			fish = new Fish(3);
			boss = new EvilBoss(4);
			Evils.evilVec.addElement(boss);
			Evils.evilVec.addElement(ninjaEvil);
			Evils.evilVec.addElement(angel);
			Evils.evilVec.addElement(flydragon);
			Evils.evilVec.addElement(fish);
			break;
			
		case 4:
			ninjaEvil = new NinjaEvil(0);
			angel = new Angel(1);
			flydragon = new Flydragon(2);
			fish = new Fish(3);
			boss = new EvilBoss(4);
			Evils.evilVec.addElement(ninjaEvil);
			Evils.evilVec.addElement(angel);
			Evils.evilVec.addElement(flydragon);
			Evils.evilVec.addElement(fish);
			Evils.evilVec.addElement(boss);
			break;

		default:
			break;
		}
	}
	
	public void checkCollisonEvil()
	{
		for(int i = 0 ; i < Evils.evilVec.size(); i++)
		{
			Evils evil = (Evils) Evils.evilVec.elementAt(i);
			if(evil.type == evil.BOSS)
			{				
				if(evil.isCollison(Boom.xD, Boom.yD, Boom.wD,Boom.hD,Boom.DAN) && !Boom.isdisappear )
				{
//					System.out.println(evil.mlife);		
					cuop.createEffect(Effect.MAU);
					evil.mlife -=1;
					if(evil.mlife==0)
					{
						EvilBoss.vecBoss.removeAllElements();
						Evils.evilVec.removeElement(evil);							
						Global.score += 50;	
						EvilBoss.mBoss--;
						if(EvilBoss.mBoss<0)
						{
							EvilBoss.mBoss = 0;
						}
					}
					Boom.mstate = Boom.DISAPPEAR;
					Boom.isdisappear = true;
				}
			}
			else
			{
				if(evil.isCollison(cuop.currentX, cuop.currentY, cuop.width, cuop.height,-1))
				{
					Evils.evilVec.removeElement(evil);
					cuop.createEffect(Effect.MAU);
					Global.score += 10;
				}
				else if(evil.isCollison(Boom.xD, Boom.yD, Boom.wD,Boom.hD,Boom.DAN))
				{					
					cuop.createEffect(Effect.MAU);
					Evils.evilVec.removeElement(evil);	
					Boom.mstate = Boom.DISAPPEAR;
					Boom.isdisappear = true; 
					Global.score += 10;
				}
			}
			
		}
	}
	
	
	public void checkCollisonActor()
	{
		for(int i=0; i< Evils.evilVec.size(); i++)
		{
			Evils evil = (Evils) Evils.evilVec.elementAt(i);
			
			
			if(cuop.mstate == cuop.state_power)
			{
				if(cuop.isCollison(evil.x,evil.y,evil.width, evil.height))
				{
					if(evil.type != Evils.BOSS)
					{
						evil.mlife--;
					}
					else
					{
						Evils.evilVec.removeElement(evil);
					}
					cuop.createEffect(Effect.MAU);
					Global.score += 10;
				}
			}

			if(cuop.mstate != cuop.state_power)
			{
				if(cuop.isCollison(evil.x,evil.y,evil.width, evil.height) )
				{									
					//evil.createEffect();
					cuop.createEffect(Effect.MAU);
					cuop.isDraw = false;
					Global.blood -= 1 ;
					if(Global.blood > 0)
					{						
						//ExGameCanvas.restMap.currentSreen();
						Level.isResetLevel = true;
						cuop.mstate = cuop.state_evil;
						removeEvils();
					}
					else if(Global.blood == 0)
					{
						cuop.isDraw = false;
						Level.isOver = true;
						removeEvils();										
						ExGameCanvas.overMap.currentSreen();
					}
				}
				
				if(cuop.isCollisonSkill(SkillsBoss.xD, SkillsBoss.yD, SkillsBoss.wD, SkillsBoss.hD) && !SkillsBoss.isdisappear)
				{			
					cuop.isDraw = false;
					Global.blood -= 1 ;
					EvilBoss.vecBoss.removeAllElements();									
					if(Global.blood > 0)
					{						
						//ExGameCanvas.restMap.currentSreen();
						Level.isResetLevel = true;
						cuop.mstate = cuop.state_evil;
						removeEvils();
					}
					else if(Global.blood == 0)
					{
						cuop.isDraw = false;
						Level.isOver = true;
						removeEvils();										
						ExGameCanvas.overMap.currentSreen();
					}
				}
						
			}
		}
	}
	

	public void checkCollisonCoins()
	{
		for(int i=0; i< TileMap.vector.size(); i ++)
		{
			Coins coin = (Coins) TileMap.vector.elementAt(i);
			if(coin.type == Coins.BOX)
			{
				if(coin.collison(cuop.currentX, cuop.currentY, cuop.width, cuop.height)&& !coin.openBox)
				{
					
					
					if(Global.isdiamond )
					{											
						Diamond diamond = new Diamond(coin.x,coin.y - 2*TileMap.size);
						coin.vectorDiamond.addElement(diamond);
					}
					
					coin.openBox = true;
				}
			}
			else
			{
				if(coin.collison(cuop.currentX, cuop.currentY, cuop.width, cuop.height))
				{
					if(coin.type == Coins.BLOOD)
					{
						Global.blood += 1;
					}
					else
					{
						Global.score += 10/2;
					}
					TileMap.vector.removeElement(coin);
				}
			}			
		}
	}
	
	
	public void checkCollisonDiamond()
	{
		for(int i=0; i< Coins.vectorDiamond.size(); i++)
		{
			Diamond dia = (Diamond) Coins.vectorDiamond.elementAt(i);
			if(dia.collison(cuop.currentX, cuop.currentY, cuop.width, cuop.height))
			{
				Global.isdiamond = false;
				Coins.vectorDiamond.removeElement(dia);
				Global.score += 20;
			}
		}
	}
	
	
	public void startGame()
	{
		
		int level = Level.currentLevel;
		EvilBoss.vecBoss.removeAllElements();
		switch (level) {
		case 1:
			initEvils();
			cuop.setPosActor(level);
			break;
			
		case 2:
//			mapTile = new TileMap(100,10,"/tile"+level+".png","/map"+level);
			mapTile = new TileMap(100,10,"/tile.png","/map0"+level);
			cuop.setPosActor(level);
			renewEvils(Level.currentLevel);
			break;
			
		case 3:
			//mapTile = new TileMap(100,10,"/tile"+level+".png","/map"+level);
			mapTile = new TileMap(100,10,"/tile.png","/map0"+level);
			cuop.setPosActor(level);
			renewEvils(Level.currentLevel);
			break;
			
		case 4:
			//mapTile = new TileMap(150,10,"/tile"+level+".png","/map"+level);
			mapTile = new TileMap(150,10,"/tile.png","/map0"+level);
			cuop.setPosActor(level);
			renewEvils(Level.currentLevel);
			break;
			
		default:
			break;
		}
		
	}
	
	public void resetGame()
	{
		cuop.isDraw = true;
//		Global.score = 0;
		int level = Level.currentLevel;
		EvilBoss.vecBoss.removeAllElements();
		
		switch (level) {
		case 1:
			if(cuop.mstate == cuop.state_sea)
			{
				cuop.setPosActor(level);
			}
			else if(cuop.mstate == cuop.state_evil)
			{
				cuop.setReset(level);
			}		
			renewEvils(Level.currentLevel);
			break;
			
		case 2:
			if(cuop.mstate == cuop.state_sea)
			{
				cuop.setPosActor(level);
			}
			else if(cuop.mstate == cuop.state_evil)
			{
				cuop.setReset(level);
			}
			renewEvils(Level.currentLevel);
			break;
			
		case 3:
			if(cuop.mstate == cuop.state_sea)
			{
				cuop.setPosActor(level);
			}
			else if(cuop.mstate == cuop.state_evil)
			{
				cuop.setReset(level);
			}
			renewEvils(Level.currentLevel);
			break;
			
		case 4:
			if(cuop.mstate == cuop.state_sea)
			{
				cuop.setPosActor(level);
			}
			else if(cuop.mstate == cuop.state_evil)
			{
				cuop.setReset(level);
			}
			renewEvils(Level.currentLevel);
			break;
			
		default:
			break;
		}
		
		Level.isResetLevel = false;
		
	}
	
	
	
	public void reStartGame()
	{
		Level.currentLevel = 1;
		int level = Level.currentLevel;
		Global.blood = 5;				
		mapTile = new TileMap(100,10,"/tile.png","/map0"+level);
		initEvils();
		cuop.isDraw = true;
		cuop.setPosActor(level);
		
	}
	
	public void paint(Graphics g)
	{
	
		g.translate(-cuop.cmX,-cuop.cmY);		
		mapTile.drawMap(g);
		cuop.draw(g);
		
		paintVectors(g);	
		
	}
	
	
	public void updateVectors()
	{
		for(int i = 0; i < cuop.effectsVec.size(); i++)
		{
			Effect eff = (Effect) cuop.effectsVec.elementAt(i);
			eff.update();
		}
		
		for(int i=0; i<cuop.vecboom.size(); i++)
		{
			Boom bom = (Boom) cuop.vecboom.elementAt(i);
			bom.update();
		}
		
		for(int i = 0; i < Evils.evilVec.size(); i++)
		{
			Evils evil = (Evils) Evils.evilVec.elementAt(i);
			evil.update();
		}
		
	}
	
	public void paintVectors(Graphics g)
	{
		for(int i = 0; i < cuop.effectsVec.size(); i++)
		{
			Effect eff = (Effect) cuop.effectsVec.elementAt(i);
			eff.paint(g);
		}
		
		for(int i=0; i<cuop.vecboom.size(); i++)
		{
			Boom bom = (Boom)cuop.vecboom.elementAt(i);
			bom.paint(g);	
		}
		
		
		for(int i = 0; i < Evils.evilVec.size(); i++)
		{
			Evils evil = (Evils) Evils.evilVec.elementAt(i);
			evil.draw(g);
		}
		
		
		for(int i=0; i<TileMap.vector.size();i++)
		{
			Coins coins = (Coins) TileMap.vector.elementAt(i);
			coins.paint(g);
		}
		
		for(int i=0; i<EvilBoss.vecBoss.size(); i++)
		{
			SkillsBoss skill = (SkillsBoss) EvilBoss.vecBoss.elementAt(i);
			skill.paint(g);
		}
		
		for(int i = 0 ; i< Coins.vectorDiamond.size(); i++)
		{
			Diamond dia = (Diamond) Coins.vectorDiamond.elementAt(i);
			dia.paint(g);
		}
	}
	
	
}
